<template>
  <div class="app-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-button type="primary" @click="handleAdd">新增角色</el-button>
          <el-table
            v-loading="loading"
            style="width: 100%"
            :data="list"
          >
            <el-table-column
              type="index"
              label="序号"
              width="80"
            />
            <el-table-column
              prop="name"
              label="角色"
              width="180"
            />
            <el-table-column
              prop="description"
              label="描述"
            />
            <el-table-column label="操作" width="320" align="right">
              <!-- scope   {row: {当前这一行的数据} $index:第几行} -->
              <template slot-scope="{row}">
                <el-button size="small" type="success">分配权限</el-button>
                <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
                <el-button size="small" type="danger" @click="handleDel(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="end" align="middle" style="height:60px">
            <el-pagination
              background
              :current-page.sync="page.page"
              :page-size.sync="page.pagesize"
              :page-sizes="[2,5,10,15]"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="getRoleList"
              @current-change="getRoleList"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input v-model="formData.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <roleDialog ref="roleDialog" :dialog-visible.sync="dialogVisible" />
    <!-- 实现弹窗组件的显示隐藏 @update:dialogVisible = -->
  </div>
</template>

<script>
import { getRoleListApi, deleteRoleApi, getCompanyInfoApi } from '@/api/setting'
import roleDialog from './components/roleDialog.vue'
import { mapGetters } from 'vuex'
export default {
  name: 'SettingIndex',
  components: {
    roleDialog
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  data() {
    return {
      activeName: 'first',
      page: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      list: [],
      loading: false,
      dialogVisible: false,
      formData: {}
    }
  },
  watch: {
    activeName(val) {
      // 新的值
      if (val === 'second' && !this.formData.id) {
        this.getCompanyInfo()
      }
    }
  },
  // 数据初始化
  // 封装请求数据的过程
  // 1. 引入接口
  // 2. 封装拿数据的过程
  // 3. created 调用
  created() {
    this.getRoleList()
    // this.getgetCompanyInfo()
  },
  methods: {
    async getRoleList() {
      try {
        this.loading = true
        const { total, rows } = await getRoleListApi(this.page)
        this.total = total
        this.list = rows
        // 判断一下
        // total > 0 并且 rows.length == 0 不正常
        // 向前请求一页数据
        if (total > 0 && rows.length === 0) {
          this.page.page--
          this.getRoleList()
        }
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    },
    handleAdd() {
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.$refs.roleDialog.roleForm = { ...row }
      // 思考两个问题
      // 为什么 这儿写 { ...row }
      // $children 不是特别合适
      console.log(this.$children)
    },
    async handleDel(id) {
      // try {
      //   await this.$confirm('是否确认删除该角色呢', '提示', {
      //     type: 'warning'
      //   })
      //   console.log('确定')
      //   // 删除接口
      // } catch (e) {
      //   console.log(e, 'err')
      // }

      // promise 链式调用
      this.$confirm('是否确认删除该角色呢', '提示', {
        type: 'warning'
      }).then(res => {
        console.log(res, '确定')
        // 删除接口
        return deleteRoleApi(id) // Promise
        // 接口调用成功之后 刷新列表页面
      }).then(res => {
        this.$message.success('删除成功')
        this.getRoleList()
      }).catch(err => {
        console.log(err, 'err')
      })
    },
    async getCompanyInfo() {
      // getCompanyInfoApi 公司ID companyId
      // getters 关系建立
      const res = await getCompanyInfoApi(this.companyId) // 利用 mapGetters
      // console.log(res)
      this.formData = res
    }
    // watch
    // 常见业务场景 类似于这样的tab切换，每当切换的时候 才初始化

    // handleSizeChange(val) {
    //   // val 当前变化的每页需要展示多少条的数据 pagesize
    //   console.log(val)
    //   // this.page.pagesize = val
    //   this.getRoleList()
    // },
    // handleCurrentChange(val) {
    //   // val 当前第几页
    //   console.log(val)
    //   this.getRoleList()
    // }
  }
}
</script>

<style lang="scss" scoped>

</style>

